#博客编辑页测试
import datetime
import time

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from Common.Untils import BlogDriver


class BlogEdit:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_edit.html"
        self.driver=BlogDriver.driver

    #未登录状态下
    def EditByNotlogin(self):
        self.driver.get(self.url)
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

    #登陆状态下
    def EditByLogin(self):
        # 输入账号密码点击提交
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        #可以通过链接访问
        self.driver.get(self.url)
        #正常提交（标题和内容都不为空）
        self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("测试1")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        #检查列表页是否显示
        #计算当前发布文章在第几个位置
        nums=self.driver.find_elements(By.CSS_SELECTOR,"body > div.container > div.right > div")
        print(len(nums))
        ret=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child("+str(len(nums))+") > div.title").text
        print(ret)
        assert ret=="测试1"
        #获取文章数量
        before=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)").text
        print(before)
        #点击“写博客”再发布一篇，测试新文章是否影响旧文章
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        # 正常提交（标题和内容都不为空）
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("测试2")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 检查列表页是否显示
        ret = self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child("+str(len(nums)+1)+") > div.title").text
        assert ret == "测试2"
        #再次获取文章数量
        after=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)").text
        print(after)
        #检查文章数量统计是否随之增加,这里有bug，数量不符合
        #assert after-before==1
        #检查发布时间是否正确
        now=datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
        print("now:"+now)
        actual=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child("+str(len(nums)+1)+") > div.date").text
        print("actual:"+actual)
        assert now==actual
        #异常提交
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        #标题为空，内容不为空
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        self.driver.switch_to.alert.accept()
        # #标题和内容都为空
        # self.driver.find_element(By.CSS_SELECTOR,"#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span").clear()
        # self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # time.sleep(1)
        # self.driver.switch_to.alert.accept()
        # #标题不为空，内容为空
        # self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("测试")
        # self.driver.find_element(By.CSS_SELECTOR,"#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span").clear()
        # self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # time.sleep(1)
        # self.driver.switch_to.alert.accept()
        #点击“主页”返回到主页，检查是否未发布
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(4)").click()
        try:
            self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child("+str(len(nums)+2)+") > div.title")
        except NoSuchElementException:
            print("标题或内容为空则会发布失败")


# Edit=BlogEdit()
# Edit.EditByNotlogin()
# Edit.EditByLogin()
